/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package app.actions;

import app.modelo.Libro;
import app.negocio.Carrito;
import app.negocio.GestionLibreria;
import app.negocio.ItfzGestionLibreria;
import app.negocio.LibroNoEncontradoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Johann
 */
public class AnadirCarritoAction extends Action{

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // Crear o recuperar la sesion del usuario
        HttpSession sesion = request.getSession();
        // Recuperar el carrito
        Carrito miCarro = (Carrito) sesion.getAttribute("carrito");
        // Si acabamos de crear la sesion el carrito no existe
        if (miCarro == null) {
            miCarro = new Carrito();
            sesion.setAttribute("carrito", miCarro);
        }
        //Recuperamos el identificador del elemento que será el isbn.
        String isbn = request.getParameter("isbn");
        Libro encontrado;
        try {
            ItfzGestionLibreria gestionLibreria = new GestionLibreria();
            encontrado = gestionLibreria.consultarISBN(isbn);
            miCarro.agregarLibro(encontrado);
        } catch (LibroNoEncontradoException ex) {
            Logger.getLogger(AnadirCarritoAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        //Variable para alterar el comportamiento del botón volver
        request.setAttribute("venimosDeAnadirCarrito", "SI");
        return mapping.findForward("ok");
    }
    
    
}
